<?php
namespace M\Action;
use Common\Model\userModel;
class LoginAction extends BaseAction
{
    public function _initialize()
    {
        parent::_initialize();
    }
    
    public function index(){
    	
    $useragent = strtolower(addslashes($_SERVER['HTTP_USER_AGENT'])); 
	if(strpos($useragent, 'micromessenger')>0 && C('yh_site_tiaozhuan')==1){
	 $this->weixinlogin();
	}
    	
     if(1 == C('yh_sms_status')){ $this->assign('sms_status',true);} 	
     if($this->visitor->is_login){
        $this->redirect(U('user/ucenter','',''));
    }	
    $this->_config_seo(array(
        'title'=>'用户登录'
        )); 

    $this->display();
}


public function findpwd(){
  if(1 == C('yh_sms_status')){ $this->assign('sms_status',true);}
   if(IS_POST){
            $code = I('verify','','trim');
            $phone = I('phone','','trim');
            if(session('sms_code') != $code || session('sms_phone') != $phone){
                $this->ajaxReturn(0, '手机验证码输入不正确');
            }
            $password = I('password','','trim');
            $repassword = I('repassword','','trim');
            if($password != $repassword){
                $this->ajaxReturn(0, '两次密码输入不一致');
            }
            $mo = new userModel();
            $where = array(
                'phone'=>$phone
            );
            $res = $mo->where($where)->setField('password', md5($password));
            if($res !== false){
                return $this->ajaxReturn(1, '密码修改成功！', U('login/index'));
            }
            $this->ajaxReturn(0, '操作失败');
        }
     $this->_config_seo(array(
            'title'=>'找回密码'
            )); 
  
  $this->display();	
	
}

public function fillphone(){
$param=I('post.');
$this->assign('memberinfo',$param);
     $track_val=cookie('trackid'); 
     if(!empty($track_val)){
        	$track=unserialize($track_val);
        	$uid=$track['t'];
        	$this->assign('recode',$track['recode']);
    	}

$this->_config_seo(array(
            'title'=>'绑定手机号'
            )); 

$this->display('User/fillphone');
}
public function savephone(){
		if(IS_POST){
			$phone = I('phone','','trim');
			$pwd = md5(I('pwd','','trim'));
			$openid=I('openid','','trim');
			$invocode=I('invocode','','trim');
		    $nickname=I('nickname','','trim');
			$mod=new userModel();
			
	    $exist = $mod->field('id,password')->where(array(
				'phone' => $phone
				))->find();
		if ($exist){
		if($pwd==$exist['password']){
			$data=array(
			//'phone'=>$phone,
			'avatar'=>I('avatar','','trim'),
			'openid'=>$openid
			);
		
		$openuid=F($openid);	
        $track_val=cookie('trackid'); 
        if($invocode || $track_val || $openuid){

        	
        	if(!empty($track_val)){
        	$track=unserialize($track_val);
        	$uid=$track['t'];
        	if($uid>0){
        		$wherex=array(
        		'id'=>$uid
        		);
        	}
        	}
        	
        	if($invocode){
        	 $wherex=array(
		'invocode' => $invocode
        	);
        	}
        	
       	if($openuid>0){
		$wherex=array(
        		'id'=>$openuid
        		);	
		}
        	
		$existx = $mod->field('id,fuid,guid')->where($wherex)->find();
        	if($existx){
        	  	$data['fuid']=$existx['id'];
        	  	$data['guid']=$existx['fuid']?$existx['fuid']:0;
        	}else{
        	$this->ajaxReturn(0, '邀请码不存在或者大小写有误！');	
        	}
        }
			
			$res=$mod->where('id='.$exist['id'])->save($data);
			//$this->reinvi($exist['id']); 直接绑定手机号不赠送积分
			$this->visitor->wechatlogin($openid);
			return $this->ajaxReturn(1, '绑定微信登录成功！');
			 }else{
			 return $this->ajaxReturn(0, '输入的账号密码不正确！');	
			 }
			}
			
			

        	  $info=array(
            'username'=>$this->remoji($nickname),
            'phone'=>$phone,
            'nickname'=>$this->remoji($nickname),
            'password'=>$pwd,
            'reg_ip'=>get_client_ip(),
            'avatar'=>I('avatar','','trim'),
            'state'=>1,
            'status'=>1,
            'reg_time'=>time(),
            'last_time'=>time(),
            'create_time'=>time(),
            'openid'=>$openid,
            );
            
       $openuid=F($openid);	   
       $track_val=cookie('trackid'); 
        if($invocode || $track_val || $openuid){
        	
        	if($openuid>0){
		$wherex=array(
        		'id'=>$openuid
        		);	
		}
        
        	if(!empty($track_val)){
        	$track=unserialize($track_val);
        	$uid=$track['t'];
        	if($uid>0){
        		$wherex=array(
        		'id'=>$uid
        		);
        	}
        	}
        	
        	if($invocode){
        	 $wherex=array(
		'invocode' => $invocode
        	);
        	}
		$existx = $mod->field('id,fuid,guid')->where($wherex)->find();
        	if($existx){
        	  	$info['fuid']=$existx['id'];
        	  	$info['guid']=$existx['fuid']?$existx['fuid']:0;
        	}else{
        	$this->ajaxReturn(0, '邀请码不存在或者大小写有误！');	
        	}
           
         }
            
        $res=$mod->add($info);
        $this->visitor->wechatlogin($openid);
			if($res){
				$this->reinvi($res);
				return $this->ajaxReturn(1, '微信登录成功！');
			}else{
				return $this->ajaxReturn(0, '微信登录失败！');
				
			}
			
		}

}




public function register()
{
  if(1 == C('yh_sms_status')){ $this->assign('sms_status',true);}
    if($this->visitor->is_login){
        $this->redirect(U('user/ucenter','',''));
    }
        
        $track_val=cookie('trackid'); 
        if(!empty($track_val)){
        	$track=unserialize($track_val);
        	$uid=$track['t'];
        	$this->assign('recode',$track['recode']);
        	if($uid>0){
        		$where=array(
        		'id'=>$uid
        		);
        	}
        	}
        

    if(IS_POST){
       $phone = trimall(I('phone','','htmlspecialchars'));

       $username = trimall(I('username','','htmlspecialchars'));

       $email = trimall(I('email','','htmlspecialchars'));

       $password =trimall(I('password','','htmlspecialchars'));

       $repassword = trimall(I('repassword','','htmlspecialchars'));
       
       $invocode = trimall(I('invocode','','htmlspecialchars'));

       $verify = I('verify','','trim');
	   
	   	if(!is_numeric($phone)){
			 $this->ajaxReturn(0, '非法操作');
		}

     if(1 == C('yh_sms_status') && (session('sms_code') != $verify || session('sms_phone') != $phone)){
           $this->ajaxReturn(0, '手机验证码输入不正确');
      	}  
        
        if($invocode || $track_val){
        	
        	if($invocode){
        	 $where=array(
		'invocode' => $invocode
        	);
        	}
        	
        	$mod=new userModel();
        	
		$exist = $mod->field('id,fuid,guid')->where($where)->find();
        	if($exist){
        	  	$data['fuid']=$exist['id'];
        	  	$data['guid']=$exist['fuid']?$exist['fuid']:0;
        	}else{
        	$this->ajaxReturn(0, '邀请码不存在或者大小写有误！');	
        	}
      
        }

    if($password != $repassword){
        $this->ajaxReturn(0, '两次密码输入不一致');
    }

    $res = $this->visitor->register($username, $phone, $email, $password, $data);

    if($res){
        return $this->ajaxReturn(1, '注册成功', U('user/ucenter'));
    }

    $this->ajaxReturn(0, $this->visitor->error);
}


$this->_config_seo(array(
    'title'=>'用户注册'
    ));

$this->display();
}


    public function pwdcode()
    {
        if(IS_POST){
            $phone = I('phone','','trim');
            $tempid=I('tempid','','trim');
            $ac=I('ac','','trim');
            $mod=new userModel();
            $res=$mod->where(array('phone'=>$phone))->find();
            if('reg' == $ac && $res){
               return $this->ajaxReturn(0, '此手机号已经被占用');
            }
            
             if('findpass' == $ac && !$res){
               return $this->ajaxReturn(0, '此手机号不存在');
            }
            
            $code = rand(100000, 999999);
            $data=array(
            'phone'=>I('phone','','trim'),
            'code'=>$code,
            //'webname'=>trim(C('yh_site_name')),
            'temp_id'=>$tempid
            );
           $res= $this->send_sms($data);
           if($res){
                session('sms_code', $code);
                session('sms_phone', $phone);
                return $this->ajaxReturn(1, '验证码发送成功');
            }
            $this->ajaxReturn(0, '验证码发送失败');
       }
       }


public function login(){

	if($this->visitor->is_login){

        $this->redirect(U('user/ucenter','',''));
    }	

    if(IS_POST){

        $username = trimall(I('username','','htmlspecialchars'));

        $password = trimall(I('password','','htmlspecialchars'));

        $remember = I('remember','0','trim');

        $verify = I('verify','','trim');

//     if($this->check_verify($verify) === false){
//        $this->ajaxReturn(0, '验证码错误');
//			 
//        }


        $res = $this->visitor->login($username, $password, $remember);

        if($res){

            if(empty($callback)){$callback=U('user/ucenter');}

            if(strpos($callback, '/login') !== false){
                $callback = U('user/ucenter');
            }

            return $this->ajaxReturn(1, '登录成功', $callback?$callback:U('user/ucenter'), array(
                'id'=>$this->visitor->get('id'),
                'nickname'=>$this->visitor->get('nickname')
                ));


        }

        $this->ajaxReturn(0, $this->visitor->error);
    }


}


public function logout()
{

   $this->visitor->logout();
   redirect('/');
   exit();

   $this->visitor->logout();
   $callback = $_SERVER['HTTP_REFERER'];
   if(
    strpos($callback, '/login') !== false ||
    strpos($callback, '/user') !== false 
    ){
    $callback = C('yh_headerm_html');
}

redirect($callback);
}

	

}  